package com.hdsx.jxcsxm.task;

import com.hdsx.jxcsxm.listener.TypeEnum;
import com.hdsx.jxcsxm.utile.HDFreeMarker;
import com.hdsx.jxcsxm.utile.MbCacheUtil;
import com.hdsx.jxcsxm.xtgl.bean.TreeNode;
import com.hdsx.jxcsxm.xtgl.server.XtglServer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

/**
 * @author 占贤银
 * time 2024/3/25 14:29
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Service
public class TaskJob {

    @Resource(name = "xtglServerImpl")
    private XtglServer xtglServer;

    public void dataCheckJob() {
        System.out.println("===========================每天23点定时任务开始=============================");
        HDFreeMarker fm = new HDFreeMarker("template/");
        List<TreeNode> xzqhtreelist = xtglServer.selAllXzqh1("36");
        List<TreeNode> gydwtreelist = xtglServer.selAllUnit1("36");
        List xzqhTreeNodes = MbCacheUtil.queryCacheTreeData(xzqhtreelist);
        List gydwTreeNodes = MbCacheUtil.queryCacheTreeData(gydwtreelist);
        MbCacheUtil.put("xzqh36", xzqhTreeNodes);
        MbCacheUtil.put("gydw36", gydwTreeNodes);
        List<TreeNode> list = xtglServer.getjhxdwhbb(TypeEnum.GS_ALL_BB.getName());
        HashMap<String,Object> root = new HashMap<String,Object>();
        HashMap<String,Object> gsAllroot = new HashMap<String,Object>(2);
        HashMap<String,Object> gsdgzroot = new HashMap<String,Object>(2);
        HashMap<String,Object> yhdzxroot = new HashMap<String,Object>(2);
        HashMap<String,Object> lwjsgzroot = new HashMap<String,Object>(2);
        root.put("list", list);
         URL url = Thread.currentThread().getContextClassLoader().getResource("/");
         String path = url.getPath().substring(0, url.getPath().indexOf("/WEB-INF/classes/"));
         File file = new File(path+"/template");
         if (!file.exists()) {
           file.mkdir();
         }
        List<TreeNode> gsAllList = xtglServer.getjhxdwh(TypeEnum.GS_ALL.getName());
        List<TreeNode> gsdgzList = xtglServer.getjhxdwh(TypeEnum.GS_GSDGZ.getName());
        List<TreeNode> yhdzxList = xtglServer.getjhxdwh(TypeEnum.GS_YHDZX.getName());
        List<TreeNode> lwjsgzList = xtglServer.getjhxdwh(TypeEnum.GS_LWJGGZ.getName());
        List<TreeNode> tsdqList = xtglServer.getTsdq();
        gsAllroot.put("list", gsAllList);
        gsdgzroot.put("list", gsdgzList);
        yhdzxroot.put("list", yhdzxList);
        lwjsgzroot.put("list", lwjsgzList);
        MbCacheUtil.put("tsdqList", tsdqList);
        fm.createJsonFile(root, "jhxdwh.ftl", path+"/template/"+ TypeEnum.GS_ALL_BB.getType() +"jhxdwh.json");
        fm.createJsonFile(gsAllroot, "jhxdwh.ftl", path+"/template/"+ TypeEnum.GS_ALL.getType() +"jhxdwh.json");
        fm.createJsonFile(gsdgzroot, "jhxdwh.ftl", path+"/template/"+ TypeEnum.GS_GSDGZ.getType() +"jhxdwh.json");
        fm.createJsonFile(yhdzxroot, "jhxdwh.ftl", path+"/template/"+ TypeEnum.GS_YHDZX.getType() +"jhxdwh.json");
        fm.createJsonFile(lwjsgzroot, "jhxdwh.ftl", path+"/template/"+ TypeEnum.GS_LWJGGZ.getType() +"jhxdwh.json");
    }
}